/*
 * XXX
 */

package edu.uhcl.csci4838.group3.view;

import java.io.File;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import edu.uhcl.csci4838.group3.R;
import edu.uhcl.csci4838.group3.database.BackpackEduDatabaseHelper;
import edu.uhcl.csci4838.group3.database.BackpackEduDatabaseHelper.CursorCourse;
import edu.uhcl.csci4838.group3.database.BackpackEduDatabaseHelper.CursorCourseCategoryWeight;
import edu.uhcl.csci4838.group3.database.info.Course;
import edu.uhcl.csci4838.group3.database.info.CourseCategoryWeight;
import edu.uhcl.csci4838.group3.database.info.Task;
import edu.uhcl.csci4838.group3.view.help.HelpActivity;
import android.app.Activity;
import android.app.ListActivity;
import android.content.ContentValues;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.SimpleAdapter;

/**
 * XXX
 * 
 * @author Chris Waguespack
 *
 */
public class ContentActivity extends ListActivity {
	/**
	 * The database helper object.
	 */
	private static BackpackEduDatabaseHelper dbHelper;

	/**
	 * The id of the course that these tasks belong to.
	 */
	private long courseId;
	
	private SimpleAdapter adapter;
	private List<HashMap<String, String>> listData;
	
	/**
	 * XXX
	 * 
	 * @param savedInstanceState
	 */
	/* 
	 * (non-Javadoc)
	 * 
	 * @see android.app.ActivityGroup#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		this.setContentView(R.layout.activity_view_content);
		
		// Calling intent
		final Intent callingIntent = this.getIntent();
		this.courseId = callingIntent.getLongExtra(this.getString(R.string.intent_extra_courseId),
				-1);
		
		// Start the database helper
		dbHelper = new BackpackEduDatabaseHelper(this);
		
		// Fill
		ContentValues criteria = new ContentValues();
		criteria.put(Course.ID,
				Long.valueOf(courseId));
		CursorCourse courseCursor = dbHelper.getCourse(criteria,
				null,
				true);
		this.startManagingCursor(courseCursor);
		String dirName = courseCursor.getColSemesterName() + File.separator + courseCursor.getColName() + File.separator;
		final File filesDir = this.getFilesDir();
		final File dir = new File(filesDir.getAbsolutePath()
				+ File.separator
				+ dirName);
		String[] files = dir.list();
		
		listData = new LinkedList<HashMap<String,String>>();
		
		if (files != null) {
			for (String file : files) {
				HashMap<String, String> map = new HashMap<String, String>(1);
				map.put("fileName", file);
				listData.add(map);
			}
		}
			
		
		// Set up the adapter
		adapter = new SimpleAdapter(this,
				listData,
				android.R.layout.simple_list_item_1,
				new String[] { "fileName" },
				new int[] { android.R.id.text1 });
		setListAdapter(adapter);
	}
	
	/**
	 * XXX
	 * 
	 * @param menu
	 *            XXX
	 * @return XXX
	 */
	/* 
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
	 */
	@Override
	public boolean onCreateOptionsMenu(final Menu menu) {
		this.getMenuInflater()
				.inflate(R.menu.options_content,
						menu);
		return true;
	}
	
	/**
	 * XXX
	 * 
	 * @param item
	 *            XXX
	 * @return XXX
	 */
	/* 
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
	 */
	@Override
	public boolean onOptionsItemSelected(final MenuItem item) {
		switch (item.getItemId()) {
			case R.id.menu_item_create_note:
				this.startActivity(new Intent(this.getApplicationContext(),
						NoteActivity.class).putExtra(this.getString(R.string.intent_extra_courseId), this.courseId));
				return true;
			case R.id.menu_item_settings:
				this.startActivity(new Intent(this.getApplicationContext(),
						SettingsActivity.class));
				return true;
			case R.id.menu_item_about:
				this.startActivity(new Intent(this.getApplicationContext(),
						AboutActivity.class));
				return true;
			case R.id.menu_item_help:
				this.startActivity(new Intent(this.getApplicationContext(),
						HelpActivity.class));
				return true;
			default:
				Log.wtf("BackpackEDU",
						"Undefined options menu item.");
				return super.onOptionsItemSelected(item);
		}
	}
}
